<?php
namespace modules\admin\service;

use idea\saf\sl\Service;
use helper\Log;
use helper\ErrorNo;
use helper\ErrorMsg;

/**
 * AdminsGroups class file
 * admins_groups表服务类
 * @author 宋欢 <iphper@yeah.net>
 * @version $Id: AdminsGroups.php 1 2013-05-18 14:58:59Z huan.song $
 * @package modules.admin.service
 * @since 1.0
 */
class AdminsGroups extends Service
{
	/**
	 * 添加一条记录
	 * @param array $params
	 * @return array
	 */
	public function add(array $params = array())
	{
		if (!isset($params['auth']) || !is_array($params['auth'])) {
			$errNo = ErrorNo::ERROR_ARGS_INSERT;
			$errMsg = ErrorMsg::ERROR_ARGS_INSERT;
			Log::warning(sprintf(
				'%s auth of params "%s" empty', $errMsg, serialize($params)
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg
			);
		}
		
		if ($params['auth'][0] === 'ALL') {
			$params['group_auth'] = 'ALL';
		}
		else {
			$params['group_auth'] = implode(',', $params['auth']);
		}

		return $this->insert($params, $this->getForm()->getRules());
	}

	/**
	 * 通过ID，更新一条记录
	 * @param integer $id
	 * @param array $params
	 * @return array
	 */
	public function edit($id , array $params = array())
	{
		if (!isset($params['auth']) || !is_array($params['auth'])) {
			$errNo = ErrorNo::ERROR_ARGS_UPDATE;
			$errMsg = ErrorMsg::ERROR_ARGS_UPDATE;
			Log::warning(sprintf(
				'%s auth of params "%s" empty', $errMsg, serialize($params)
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg,
				'id' => $id
			);
		}

		if ($params['auth'][0] === 'ALL') {
			$params['group_auth'] = 'ALL';
		}
		else {
			$params['group_auth'] = implode(',', $params['auth']);
		}

		return $this->updateByPk($id, $params, $this->getForm()->getRules());
	}
}
